package com.yanfei.zero.application.handler.secret;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.Charset;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter;

import com.yanfei.zero.infrastructure.config.DeveloperProperty;

import cn.hutool.crypto.asymmetric.AsymmetricAlgorithm;
import cn.hutool.crypto.asymmetric.AsymmetricCrypto;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.json.JSONObject;
import lombok.RequiredArgsConstructor;
/**
 * 请求数据统一处理
 */
@RestControllerAdvice
@RequiredArgsConstructor
public class DecryptRequest extends RequestBodyAdviceAdapter {

    final DeveloperProperty developerProperty;

	
    @Override
    public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        return !methodParameter.hasMethodAnnotation(IgnoreDecrypt.class);
    }
 
    @Override
    public HttpInputMessage beforeBodyRead(final HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
    	String body = StreamUtils.copyToString(inputMessage.getBody(), Charset.defaultCharset());
    	System.out.println("解密前:  " +body);
        try {
        	
        	AsymmetricCrypto decrypt = new AsymmetricCrypto(AsymmetricAlgorithm.RSA,developerProperty.getPrivateKey(),developerProperty.getPublicKey());
        	String parm  =  decrypt.decryptStr(body, KeyType.PrivateKey);
        	
        	// 记录日志
        	System.out.println("解密后:  " + parm);
        	
        	return new HttpInputMessage() {
    			
    			@Override
    			public HttpHeaders getHeaders() {
    				return inputMessage.getHeaders();
    			}
    			
    			@Override
    			public InputStream getBody() throws IOException {
    				return new ByteArrayInputStream(parm.getBytes());
    			}
    		};
        	
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.beforeBodyRead(inputMessage, parameter, targetType, converterType);
    }
    
    public static void main(String[] args) {
    	String publicKey ="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDFSElJYWJqx2yvOy9Ia6lmkbvV76xXgYBFHKhtPnyZBGFLhu3y9ZPqwaTKFNSoxTLpWE+sOrUoxxg10xuZmt5uW7sihakDvunNNoFCWaDxqu1U9X/SfQFYJfNFKfMMUQ2SVCLDCWjXwgd9Frn6+3BIfPbJIms4J3fTEJJ1DFxE+QIDAQAB";
    	String privateKey ="MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAMVISUlhYmrHbK87L0hrqWaRu9XvrFeBgEUcqG0+fJkEYUuG7fL1k+rBpMoU1KjFMulYT6w6tSjHGDXTG5ma3m5buyKFqQO+6c02gUJZoPGq7VT1f9J9AVgl80Up8wxRDZJUIsMJaNfCB30Wufr7cEh89skiazgnd9MQknUMXET5AgMBAAECgYBEpB6PsBkQovuPSq3saj1CKVGsIZyaw72wDkUR4YxzpyD9KODQyJ5jL2YZQ78sS5SnuOjbuX9/xSoIzD+NpeNe8RoA0Z9KcG2JtD55gl6P6IZPq561F3/ypEGHOQcFE+UaY6gI8C5Xy5apUexT7k8PMekFc5tlyj52KwdeknKgowJBAOMC6pBJynY5HDQ+DkG736ewdIU1lgskb+cp3LEHxQOGgRJ/B1UKXG5qr8ppynJKHsUl9ZkNPzMkcXshQca7xCcCQQDeeYSAbSZkcAX7S1QxT8LyWDTNsWtr0RswvAU0OZQ1KGCGtTwb7vofkLvvUdYBr6cD+HbXO4Yp+0eQz4nMncHfAkB6EQ9gFs2vVHY+AUBZSFSzLsb4+h98TlN90ZzB9JOTmbe94mBm8fNRzDxKcyhQ3Fo/WHH/oofhUC9VLkTYQA7xAkEAvWhPutcyvIfzT8MOZv01SAZ4zjai8iQRNiiddZ0f5TH/HDDogA+xu3YJyvKtdLoLQ4IiC5SN4OgiQdOvyhj1mQI/CaFXSrD6um+iFjvsr/c7JFx747o6hHb3HUCryO0DeXAqtebKBC6uc9ZgJEPY4PX8AetwQE2ucHJH9BYscBaV";
    	AsymmetricCrypto rsa = new AsymmetricCrypto(AsymmetricAlgorithm.RSA,privateKey,publicKey);
//    	System.out.println(rsa.getPublicKeyBase64());
//       	System.out.println("=====================");
    	//获得私钥
//    	System.out.println(rsa.getPrivateKeyBase64());
    	JSONObject jsn = new JSONObject();
    	jsn.set("id", "1");
    	jsn.set("name", "1");
    	jsn.set("email", "819862180@");
    	String enc = rsa.encryptBase64(jsn.toString(), KeyType.PublicKey);
    	System.out.println(enc);
    	System.out.println("=====================");
    	String decry = rsa.decryptStr(enc, KeyType.PrivateKey);
    	System.out.println("decry:"+decry);
	}
}
